﻿

using System.Data.Entity;
using Core.Config;
using Core.Log.Model;

namespace Core.Log
{
    /// <summary>
    /// 记录操作日志实现类
    /// </summary>
    public class LogDbContext : DbContext, IAuditable
    {
        public LogDbContext()
            : base(CachedConfigContext.Current.DaoConfig.MyDbLog)
        {
            Database.SetInitializer<LogDbContext>(null);
        }

        /// <summary>
        /// 操作记录表数据集
        /// </summary>
        public DbSet<AuditLog> AuditLog { get; set; }

        /// <summary>
        /// 记录操作日志
        /// </summary>
        public void WriteLog(int modelId, string userName, string moduleName, string tableName, string eventType, string newValues)
        {
            using (var log = new LogDbContext())
            {
                log.AuditLog.Add(new AuditLog
                {
                    ModelId = modelId,
                    UserName = userName,
                    ModuleName = moduleName,
                    TableName = tableName,
                    EventType = eventType,
                    NewValues = newValues
                });
                SaveChanges();
            }
        }
    }
}
